package com.mm.serivce.user.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mm.domain.user.*;
import com.mm.mapper.user.*;
import com.mm.serivce.user.IDPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mm.util.common.CommonUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 权限/菜单信息表 服务实现类
 * </p>
 *
 * @author JueDream
 * @since 2021-08-04
 */
@Service
public class DPermissionServiceImpl extends ServiceImpl<DPermissionMapper, DPermission> implements IDPermissionService {

    private final DUserMapper userMapper;

    private final DRoleMapper roleMapper;

    private final DPermissionMapper permissionMapper;

    private final DUserRoleRelationMapper userRoleRelationMapper;

    private final DRolePermissionRelationMapper rolePermissionRelationMapper;

    private final DUserPermissionRelationMapper userPermissionRelationMapper;

    public DPermissionServiceImpl(DUserMapper userMapper, DRoleMapper roleMapper, DPermissionMapper permissionMapper, DUserRoleRelationMapper userRoleRelationMapper, DRolePermissionRelationMapper rolePermissionRelationMapper, DUserPermissionRelationMapper userPermissionRelationMapper) {
        this.userMapper = userMapper;
        this.roleMapper = roleMapper;
        this.permissionMapper = permissionMapper;
        this.userRoleRelationMapper = userRoleRelationMapper;
        this.rolePermissionRelationMapper = rolePermissionRelationMapper;
        this.userPermissionRelationMapper = userPermissionRelationMapper;
    }

    @Override
    public Page<DPermission> queryPermissionPage(Integer page, Integer limit, String pName) {
        Page<DPermission> permissionIPage = new Page<>(page, limit);
        LambdaQueryWrapper<DPermission> queryWrapper = new LambdaQueryWrapper<DPermission>();
        if (StrUtil.isNotBlank(pName)) {
            queryWrapper.like(DPermission::getPName, pName);
        }
        queryWrapper.eq(DPermission::getDelFlg, '0');
        permissionIPage = permissionMapper.selectPage(permissionIPage, queryWrapper);
        permissionIPage.setTotal(permissionIPage.getRecords().size());
        return permissionIPage;
    }

    @Override
    public List<DPermission> queryPermissionList(String pName) {
        LambdaQueryWrapper<DPermission> queryWrapper = new LambdaQueryWrapper<DPermission>();
        if (StrUtil.isNotBlank(pName)) {
            queryWrapper.like(DPermission::getPName, pName);
        }
        queryWrapper.eq(DPermission::getDelFlg, '0');
        // 递归查询权限菜单
        List<DPermission> permisss = permissionMapper.selectList(queryWrapper);
        permisss = permisss.stream().distinct().collect(Collectors.toList());
        // 递归树形菜单
        permisss = CommonUtils.mergeMenuList(permisss);
        return permisss;
    }

    @Override
    public List<DPermission> queryMenuByUser(Integer userId, Integer type) {
        List<DPermission> permisss = new ArrayList<>();
        LambdaQueryWrapper<DUser> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DUser::getUserId, userId);
        queryWrapper.eq(DUser::getDelFlg, "0");
        DUser user = userMapper.selectOne(queryWrapper);
        if (Objects.nonNull(user)) {
            // 根据用户权限关系查询用户权限菜单
            LambdaQueryWrapper<DUserPermissionRelation> queryURWrapper = new LambdaQueryWrapper();
            queryURWrapper.eq(DUserPermissionRelation::getUserId, user.getUserId());
            List<DUserPermissionRelation> userRoleRelations = userPermissionRelationMapper.selectList(queryURWrapper);

            List<Integer> pIds = userRoleRelations.stream().map(rp -> rp.getPId()).collect(Collectors.toList());
            LambdaQueryWrapper<DPermission> queryPWrapper = new LambdaQueryWrapper();
            queryPWrapper.in(DPermission::getDelFlg, "0");
            if (pIds.size() > 0) {
                // 查询用户权限
                queryPWrapper.in(DPermission::getPId, pIds);
            }
            permisss = permissionMapper.selectList(queryPWrapper);
            // 递归查询权限菜单
            permisss = permisss.stream().distinct().collect(Collectors.toList());
            if (type.equals(0)){
                permisss = CommonUtils.mergeMenuList(permisss);
            }
        }
        return permisss;
    }

    @Override
    public List<DPermission> queryMenu() {
        LambdaQueryWrapper<DPermission> queryPWrapper = new LambdaQueryWrapper();
        queryPWrapper.in(DPermission::getDelFlg, "0");
        List<DPermission> permisss = permissionMapper.selectList(queryPWrapper);
        // 递归查询权限菜单
        permisss = permisss.stream().distinct().collect(Collectors.toList());
        permisss = CommonUtils.mergeMenuList(permisss);
        return permisss;
    }

    @Override
    public List<DPermission> queryMenuAll() {
        LambdaQueryWrapper<DPermission> queryPWrapper = new LambdaQueryWrapper();
        queryPWrapper.in(DPermission::getDelFlg, "0");
        List<DPermission> permisss = permissionMapper.selectList(queryPWrapper);
        // 递归查询权限菜单
        permisss = permisss.stream().distinct().collect(Collectors.toList());
        return permisss;
    }

    @Override
    public List<DPermission> queryMenuByRole(List<Integer> roleIds) {
        List<DPermission> permisss = new ArrayList<>();
        LambdaQueryWrapper<DRolePermissionRelation> queryRPWrapper = new LambdaQueryWrapper();
        queryRPWrapper.in(DRolePermissionRelation::getRoleId, roleIds);
        List<DRolePermissionRelation> rolePermissionRelations = rolePermissionRelationMapper.selectList(queryRPWrapper);
        if (rolePermissionRelations.size() > 0){
            List<Integer> permissIds = rolePermissionRelations.stream().map(rp -> rp.getPId()).collect(Collectors.toList());
            LambdaQueryWrapper<DPermission> queryPWrapper = new LambdaQueryWrapper();
            queryPWrapper.in(DPermission::getPId, permissIds);
            permisss = permissionMapper.selectList(queryPWrapper);
            // 去重  递归查询权限菜单
            permisss = permisss.stream().distinct().collect(Collectors.toList());
            permisss = CommonUtils.mergeMenuList(permisss);
        }
        return permisss;
    }

    @Override
    public DPermission queryPermissionOne(Integer pId) {
        DPermission permissionInfo = new DPermission();
        if (pId > 0) {
            permissionInfo = permissionMapper.selectById(pId);
            if (Objects.nonNull(permissionInfo) && permissionInfo.pParentId > 0){
                DPermission pInfo = permissionMapper.selectById(permissionInfo.pParentId);
                permissionInfo.setPParentName(pInfo.pName);
            }else{
                permissionInfo.setPParentName("系统权限根节点");
            }
        }
        return permissionInfo;
    }

    @Override
    public boolean removePermission(Integer pId) {
        boolean flag = false;
        DPermission permissionInfo = permissionMapper.selectById(pId);
        if (Objects.nonNull(permissionInfo)) {
            permissionInfo.setDelFlg(1);
            Integer result = permissionMapper.updateById(permissionInfo);
            if (result > 0) {
                flag = true;
            }
        }
        return flag;
    }


}
